//
//  PKRadioDetailTableViewController.m
//  PianKe
//
//  Created by luojinrong11 on 15/10/4.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKRadioDetailTableViewController.h"
#import "PKRequestManager.h"
#import "PKRadioDetailModel.h"
#import "PKRadioDetailTableViewCell.h"
#import "PKRadioDetailHeaderView.h"
#import "PKRadioInfo.h"
#import "PKPKPlayViewController.h"

@interface PKRadioDetailTableViewController ()
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)NSMutableArray *musicUrlArray;
@property(nonatomic,strong) PKPKPlayViewController *playViewController;
@property(nonatomic,strong)  PKRadioInfo *radioInfoModel;
@end

@implementation PKRadioDetailTableViewController
-(NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray==nil) {
        _dataSourceArray=[NSMutableArray arrayWithCapacity:0];
        
    }
    return _dataSourceArray;
}
-(NSMutableArray *)musicUrlArray
{
    if (_musicUrlArray==nil) {
        self.musicUrlArray=[NSMutableArray arrayWithCapacity:0];
        
    }
    return _musicUrlArray;
}
-(void)acquireData
{
    NSMutableDictionary *parDic=[NSMutableDictionary dictionary];
    parDic[@"auth"]=@"";
    parDic[@"deviceid"]=@"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"radioid"]=self.radioid;
    parDic[@"version"]=@"3.0.4";
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/ting/radio_detail" patDic:parDic finish:^(NSData *data) {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *listArray=dict[@"data"][@"list"];
        for(NSDictionary *modelDict in listArray)
        {
            PKRadioDetailModel *detailModel=[[PKRadioDetailModel alloc]init];
            [detailModel setValuesForKeysWithDictionary:modelDict];
            [self.dataSourceArray addObject:detailModel];
            
        }
             [self.tableView reloadData];
        PKRadioDetailHeaderView *detailHeaderView=[[PKRadioDetailHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        NSDictionary *raioInfoDict=dict[@"data"][@"radioInfo"];
        NSLog(@"%@",raioInfoDict);
      self.radioInfoModel=[[PKRadioInfo alloc]init];
        [self.radioInfoModel setValuesForKeysWithDictionary:raioInfoDict];
        detailHeaderView.radioInfo=self.radioInfoModel;
        self.tableView.tableHeaderView=detailHeaderView;
   
    } Error:^(NSError *error) {
        NSLog(@"详情界面的数据请求失败：%@",error);
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden=NO;

    [self acquireData];
    [self.tableView registerClass:[PKRadioDetailTableViewCell class] forCellReuseIdentifier:@"PKRadioDetailTableViewCell"];
    self.tableView.rowHeight=80;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1 ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.dataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   PKRadioDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PKRadioDetailTableViewCell"forIndexPath:indexPath];
    
    // Configure the cell...
    cell.detailModel=self.dataSourceArray[indexPath.row];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  self.playViewController=[[PKPKPlayViewController alloc]init];
    /**  遍历一下数组*/
    for (int i =0;i<_dataSourceArray.count;i++) {
        PKRadioDetailModel *model=_dataSourceArray[i];
        if (i==indexPath.row) {
            model.isSelected=YES;
        }
        else
        {
            model.isSelected=NO;
        }
            
    }
    self.playViewController.dataSourceArray=self.dataSourceArray;
    self.playViewController.radioModel=self.dataSourceArray[indexPath.row];
#warning 获取所有音频的url
    for (int i=0; i<self.dataSourceArray.count; i++) {
        NSString *musicUrl=((PKRadioDetailModel *)self.dataSourceArray[i]).musicUrl;
        [self.musicUrlArray addObject:musicUrl];
    }
    self.playViewController.musicArray=self.musicUrlArray;
    self.playViewController.songNumber=indexPath.row;
//    playViewController.musicUrl=((PKRadioDetailModel *)self.dataSourceArray[indexPath.row]).musicUrl;
    self.playViewController.radionInfoModel=self.radioInfoModel;
  
    [self.navigationController pushViewController:self.playViewController animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
